﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LightningForms
{
    public class Date : BaseTextControl
    {
        private string script()
        {
            return "_lf.AddControl({ Control: '#_R_', Type: _lf.Type.DATE });".Replace("_R_", this.ClientID);
        }

        public new DateTime? MinValue
        {
            get { return string.IsNullOrEmpty(base.MinValue) ? null : new Nullable<DateTime>(Convert.ToDateTime(base.MinValue)); }
            set { if (value.HasValue)  base.MinValue = value.Value.ToShortDateString(); }
        }
        public new DateTime? MaxValue
        {
            get { return string.IsNullOrEmpty(base.MaxValue) ? null : new Nullable<DateTime>(Convert.ToDateTime(base.MaxValue)); }
            set { if(value.HasValue) base.MaxValue = value.Value.ToShortDateString(); }
        }

        private void Page_Init(object sender, EventArgs e)
        {
            //ADDING AN HANDLER BUTTON (AS A SPAN)
            Helper.AddSiblingControl(
                this,
                new LiteralControl(
                    string.Format(
                        "<input type=\"button\" id=\"{0}_bt\" class=\"ui-state-default ui-corner-all ui-icon ui-icon-calendar\" style=\"margin-left:.3em;display:inline\" />",
                        this.ClientID)));
        }

        public Date()
        {
            this.DataType = ValidationDataType.Date;
        }

        protected override void OnInit(EventArgs e)
        {
            this.Page.Init += Page_Init;
            base.OnInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), this.ClientID, script(), true);
            base.OnPreRender(e);
        }
    }
}